using System;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
namespace APP.HELPER.Utils.BarcodeLib
{
	public class BarcodeEdit : IDisposable
	{
		private string Encoded_Value = "";
		private string Formatted_Data = "";
		private Color _BackColor = Color.White;
		private string _Country_Assigning_Manufacturer_Code = "N/A";
		private Image _Encoded_Image;
		private double _EncodingTime;
		private Color _ForeColor = Color.Black;
		private int _Height = 150;
		private ImageFormat _ImageFormat = ImageFormat.Jpeg;
		private int _Width = 300;
		private string _XML = "";
		private Font _font = new Font("Tahoma", 8f, FontStyle.Regular);
		private BarCodeStyles _styles = BarCodeStyles.UNSPECIFIED;
		private Font _textFont = new Font("Tahoma", 8f, FontStyle.Regular);
		public string FormattedData
		{
			get
			{
				return this.Formatted_Data;
			}
		}
		public string EncodedValue
		{
			get
			{
				return this.Encoded_Value;
			}
		}
		public string CAM_Code
		{
			get
			{
				return this._Country_Assigning_Manufacturer_Code;
			}
		}
		public BarCodeStyles Styles
		{
			get
			{
				return this._styles;
			}
			set
			{
				this._styles = value;
			}
		}
		public Image EncodedImage
		{
			get
			{
				return this._Encoded_Image;
			}
		}
		public Color ForeColor
		{
			get
			{
				return this._ForeColor;
			}
			set
			{
				this._ForeColor = value;
			}
		}
		public Color BackColor
		{
			get
			{
				return this._BackColor;
			}
			set
			{
				this._BackColor = value;
			}
		}
		public int Width
		{
			get
			{
				return this._Width;
			}
			set
			{
				this._Width = value;
			}
		}
		public int Height
		{
			get
			{
				return this._Height;
			}
			set
			{
				this._Height = value;
			}
		}
		public bool ShowText
		{
			get;
			set;
		}
		public double EncodingTime
		{
			get
			{
				return this._EncodingTime;
			}
			set
			{
				this._EncodingTime = value;
			}
		}
		public string XML
		{
			get
			{
				return this._XML;
			}
		}
		public ImageFormat ImageFormat
		{
			get
			{
				return this._ImageFormat;
			}
			set
			{
				this._ImageFormat = value;
			}
		}
		public Font TextFont
		{
			get
			{
				return this._textFont;
			}
			set
			{
				this._textFont = value;
			}
		}
		public Font Font
		{
			get
			{
				return this._font;
			}
			set
			{
				this._font = value;
			}
		}
		public bool ShowTitle
		{
			get;
			set;
		}
		public string Title
		{
			get;
			set;
		}
		public string Text
		{
			get;
			set;
		}
		public BarcodeEdit()
		{
			this.Text = "";
			this.Title = "";
		}
		public BarcodeEdit(string data)
		{
			this.Title = "";
			this.Text = data;
		}
		public BarcodeEdit(string data, BarCodeStyles iBarCodeStyles)
		{
			this.Title = "";
			this.Text = data;
			this._styles = iBarCodeStyles;
		}
		public void Update()
		{
			this.Encode();
		}
		public Image Encode(BarCodeStyles iBarCodeStyles, string StringToEncode, int Width, int Height)
		{
			this.Width = Width;
			this.Height = Height;
			return this.Encode(iBarCodeStyles, StringToEncode);
		}
		public Image Encode(BarCodeStyles iBarCodeStyles, string StringToEncode, Color ForeColor, Color BackColor, int Width, int Height)
		{
			this.Width = Width;
			this.Height = Height;
			return this.Encode(iBarCodeStyles, StringToEncode, ForeColor, BackColor);
		}
		public Image Encode(BarCodeStyles iBarCodeStyles, string StringToEncode, Color ForeColor, Color BackColor)
		{
			this.BackColor = BackColor;
			this.ForeColor = ForeColor;
			return this.Encode(iBarCodeStyles, StringToEncode);
		}
		public Image Encode(BarCodeStyles iBarCodeStyles, string stringToEncode)
		{
			this.Text = stringToEncode;
			return this.Encode(iBarCodeStyles);
		}
		internal Image Encode(BarCodeStyles iBarCodeStyles)
		{
			this._styles = iBarCodeStyles;
			return this.Encode();
		}
		internal Image Encode()
		{
			DateTime now = DateTime.Now;
			if (this.Text.Trim() == "")
			{
				throw new Exception("EENCODE-1: Input data not allowed to be blank.");
			}
			if (this.Styles == BarCodeStyles.UNSPECIFIED)
			{
				throw new Exception("EENCODE-2: Symbology type not allowed to be unspecified.");
			}
			this.Encoded_Value = "";
			this._Country_Assigning_Manufacturer_Code = "N/A";
			IBarcode barcode;
			switch (this._styles)
			{
			case BarCodeStyles.UPCA:
			case BarCodeStyles.UCC12:
				barcode = new UPCA(this.Text);
				break;
			case BarCodeStyles.UPCE:
				barcode = new UPCE(this.Text);
				break;
			case BarCodeStyles.UPC_SUPPLEMENTAL_2DIGIT:
				barcode = new UPCSupplement2(this.Text);
				break;
			case BarCodeStyles.UPC_SUPPLEMENTAL_5DIGIT:
				barcode = new UPCSupplement5(this.Text);
				break;
			case BarCodeStyles.EAN13:
			case BarCodeStyles.UCC13:
				barcode = new EAN13(this.Text);
				break;
			case BarCodeStyles.EAN8:
				barcode = new EAN8(this.Text);
				break;
			case BarCodeStyles.Interleaved2of5:
				barcode = new Interleaved2of5(this.Text);
				break;
			case BarCodeStyles.Standard2of5:
			case BarCodeStyles.Industrial2of5:
				barcode = new Standard2of5(this.Text);
				break;
			case BarCodeStyles.CODE39:
			case BarCodeStyles.LOGMARS:
				barcode = new Code39(this.Text);
				break;
			case BarCodeStyles.CODE39Extended:
				barcode = new Code39(this.Text, true);
				break;
			case BarCodeStyles.Codabar:
				barcode = new Codabar(this.Text);
				break;
			case BarCodeStyles.PostNet:
				barcode = new Postnet(this.Text);
				break;
			case BarCodeStyles.BOOKLAND:
			case BarCodeStyles.ISBN:
				barcode = new ISBN(this.Text);
				break;
			case BarCodeStyles.JAN13:
				barcode = new JAN13(this.Text);
				break;
			case BarCodeStyles.MSI_Mod10:
			case BarCodeStyles.MSI_2Mod10:
			case BarCodeStyles.MSI_Mod11:
			case BarCodeStyles.MSI_Mod11_Mod10:
			case BarCodeStyles.Modified_Plessey:
				barcode = new MSI(this.Text, this._styles);
				break;
			case BarCodeStyles.CODE11:
			case BarCodeStyles.USD8:
				barcode = new Code11(this.Text);
				break;
			case BarCodeStyles.CODE128:
				barcode = new Code128(this.Text);
				break;
			case BarCodeStyles.CODE128A:
				barcode = new Code128(this.Text, Code128.TYPES.A);
				break;
			case BarCodeStyles.CODE128B:
				barcode = new Code128(this.Text, Code128.TYPES.B);
				break;
			case BarCodeStyles.CODE128C:
				barcode = new Code128(this.Text, Code128.TYPES.C);
				break;
			case BarCodeStyles.ITF14:
				barcode = new ITF14(this.Text);
				break;
			case BarCodeStyles.CODE93:
				barcode = new Code93(this.Text);
				break;
			default:
				throw new Exception("EENCODE-2: Unsupported encoding type specified.");
			}
			this.Encoded_Value = barcode.Encoded_Value;
			this.Text = barcode.RawData;
			this.Formatted_Data = barcode.FormattedData;
			this._EncodingTime = (DateTime.Now - now).TotalMilliseconds;
			this._Encoded_Image = this.Generate_Image();
			this._XML = this.GetXML();
			return this.EncodedImage;
		}
		private Bitmap Generate_Image()
		{
			if (this.Encoded_Value == "")
			{
				throw new Exception("EGENERATE_IMAGE-1: Must be encoded first.");
			}
			Bitmap bitmap = null;
			BarCodeStyles styles = this._styles;
			if (styles != BarCodeStyles.PostNet)
			{
				if (styles != BarCodeStyles.ITF14)
				{
					bitmap = new Bitmap(this.Width, this.Height);
					int num = this.Width / this.Encoded_Value.Length;
					int num2 = this.Width % this.Encoded_Value.Length / 2;
					if (num <= 0)
					{
						throw new Exception("EGENERATE_IMAGE-2: Image size specified not large enough to draw image. (Bar size determined to be less than 1 pixel)");
					}
					int i = 0;
					using (Graphics graphics = Graphics.FromImage(bitmap))
					{
						graphics.Clear(this.BackColor);
						Pen pen = new Pen(this.ForeColor, (float)num);
						pen.Alignment = PenAlignment.Right;
						while (i < this.Encoded_Value.Length)
						{
							if (this.Encoded_Value[i] == '1')
							{
								graphics.DrawLine(pen, new Point(i * num + num2, 0), new Point(i * num + num2, this.Height));
							}
							i++;
						}
					}
					if (this.ShowText)
					{
						this.Label_Generic(bitmap);
					}
				}
				else
				{
					bitmap = new Bitmap(this.Width, this.Height);
					int num3 = (int)((double)bitmap.Width / 12.05);
					int num4 = Convert.ToInt32((double)bitmap.Width * 0.05);
					int num = (bitmap.Width - num3 * 2 - num4 * 2) / this.Encoded_Value.Length;
					int num2 = (bitmap.Width - num3 * 2 - num4 * 2) % this.Encoded_Value.Length / 2;
					if (num <= 0 || num4 <= 0)
					{
						throw new Exception("EGENERATE_IMAGE-3: Image size specified not large enough to draw image. (Bar size determined to be less than 1 pixel or quiet zone determined to be less than 1 pixel)");
					}
					int i = 0;
					using (Graphics graphics = Graphics.FromImage(bitmap))
					{
						graphics.Clear(this.BackColor);
						Pen pen = new Pen(this.ForeColor, (float)num);
						pen.Alignment = PenAlignment.Right;
						while (i < this.Encoded_Value.Length)
						{
							if (this.Encoded_Value[i] == '1')
							{
								graphics.DrawLine(pen, new Point(i * num + num2 + num3 + num4, 0), new Point(i * num + num2 + num3 + num4, this.Height));
							}
							i++;
						}
						pen = new Pen(this.ForeColor, (float)bitmap.Height / 8f);
						pen.Alignment = PenAlignment.Inset;
						graphics.DrawLine(pen, new Point(0, 0), new Point(bitmap.Width, 0));
						graphics.DrawLine(pen, new Point(0, bitmap.Height), new Point(bitmap.Width, bitmap.Height));
						graphics.DrawLine(pen, new Point(0, 0), new Point(0, bitmap.Height));
						graphics.DrawLine(pen, new Point(bitmap.Width, 0), new Point(bitmap.Width, bitmap.Height));
					}
					if (this.ShowText)
					{
						this.Label_ITF14(bitmap);
					}
				}
			}
			else
			{
				bitmap = new Bitmap(this.Encoded_Value.Length * 4, 20);
				for (int j = bitmap.Height - 1; j > 0; j--)
				{
					int k = 0;
					if (j < bitmap.Height / 2)
					{
						while (k < bitmap.Width)
						{
							if (this.Encoded_Value[k / 4] == '1')
							{
								bitmap.SetPixel(k, j, this.ForeColor);
								bitmap.SetPixel(k + 1, j, this.ForeColor);
								bitmap.SetPixel(k + 2, j, this.BackColor);
								bitmap.SetPixel(k + 3, j, this.BackColor);
							}
							else
							{
								bitmap.SetPixel(k, j, this.BackColor);
								bitmap.SetPixel(k + 1, j, this.BackColor);
								bitmap.SetPixel(k + 2, j, this.BackColor);
								bitmap.SetPixel(k + 3, j, this.BackColor);
							}
							k += 4;
						}
					}
					else
					{
						while (k < bitmap.Width)
						{
							bitmap.SetPixel(k, j, this.ForeColor);
							bitmap.SetPixel(k + 1, j, this.ForeColor);
							bitmap.SetPixel(k + 2, j, this.BackColor);
							bitmap.SetPixel(k + 3, j, this.BackColor);
							k += 4;
						}
					}
				}
			}
			this._Encoded_Image = bitmap;
			return bitmap;
		}
		public byte[] GetImageData(SaveTypes savetype)
		{
			byte[] result = null;
			try
			{
				if (this._Encoded_Image != null)
				{
					using (MemoryStream memoryStream = new MemoryStream())
					{
						this.SaveImage(memoryStream, savetype);
						result = memoryStream.ToArray();
						memoryStream.Flush();
						memoryStream.Close();
					}
				}
			}
			catch (Exception ex)
			{
				throw new Exception("EGETIMAGEDATA-1: Could not retrieve image data. " + ex.Message);
			}
			return result;
		}
		public void SaveImage(string Filename, SaveTypes FileType)
		{
			try
			{
				if (this._Encoded_Image != null)
				{
					ImageFormat format;
					switch (FileType)
					{
					case SaveTypes.JPG:
						format = ImageFormat.Jpeg;
						break;
					case SaveTypes.BMP:
						format = ImageFormat.Bmp;
						break;
					case SaveTypes.PNG:
						format = ImageFormat.Png;
						break;
					case SaveTypes.GIF:
						format = ImageFormat.Gif;
						break;
					case SaveTypes.TIFF:
						format = ImageFormat.Tiff;
						break;
					default:
						format = this.ImageFormat;
						break;
					}
					((Bitmap)this._Encoded_Image).Save(Filename, format);
				}
			}
			catch (Exception ex)
			{
				throw new Exception("ESAVEIMAGE-1: Could not save image.\n\n=======================\n\n" + ex.Message);
			}
		}
		public void SaveImage(Stream stream, SaveTypes FileType)
		{
			try
			{
				if (this._Encoded_Image != null)
				{
					ImageFormat format;
					switch (FileType)
					{
					case SaveTypes.JPG:
						format = ImageFormat.Jpeg;
						break;
					case SaveTypes.BMP:
						format = ImageFormat.Bmp;
						break;
					case SaveTypes.PNG:
						format = ImageFormat.Png;
						break;
					case SaveTypes.GIF:
						format = ImageFormat.Gif;
						break;
					case SaveTypes.TIFF:
						format = ImageFormat.Tiff;
						break;
					default:
						format = this.ImageFormat;
						break;
					}
					((Bitmap)this._Encoded_Image).Save(stream, format);
				}
			}
			catch (Exception ex)
			{
				throw new Exception("ESAVEIMAGE-2: Could not save image.\n\n=======================\n\n" + ex.Message);
			}
		}
		private void Label_ITF14(Image img)
		{
			try
			{
				Font font = this._font;
				using (Graphics graphics = Graphics.FromImage(img))
				{
					graphics.DrawImage(img, 0f, 0f);
					graphics.SmoothingMode = SmoothingMode.HighQuality;
					graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
					graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
					graphics.CompositingQuality = CompositingQuality.HighQuality;
					graphics.FillRectangle(new SolidBrush(this.BackColor), new Rectangle(0, img.Height - 16, img.Width, 16));
					StringFormat stringFormat = new StringFormat();
					stringFormat.Alignment = StringAlignment.Center;
					graphics.DrawString(this.Text, font, new SolidBrush(this.ForeColor), (float)(img.Width / 2), (float)(img.Height - 16), stringFormat);
					graphics.DrawLine(new Pen(this.ForeColor, (float)img.Height / 16f)
					{
						Alignment = PenAlignment.Inset
					}, new Point(0, img.Height - 20), new Point(img.Width, img.Height - 20));
					graphics.Save();
				}
			}
			catch (Exception ex)
			{
				throw new Exception("ELABEL_ITF14-1: " + ex.Message);
			}
		}
		private void Label_Generic(Image img)
		{
			try
			{
				Font textFont = this._textFont;
				using (Graphics graphics = Graphics.FromImage(img))
				{
					graphics.DrawImage(img, 0f, 0f);
					graphics.SmoothingMode = SmoothingMode.HighQuality;
					graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
					graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
					graphics.CompositingQuality = CompositingQuality.HighQuality;
					graphics.FillRectangle(new SolidBrush(this.BackColor), new Rectangle(0, img.Height - 16, img.Width, 16));
					StringFormat stringFormat = new StringFormat();
					stringFormat.Alignment = StringAlignment.Center;
					string s = (this.FormattedData.Trim() != "") ? this.FormattedData : this.Text;
					graphics.DrawString(s, textFont, new SolidBrush(this.ForeColor), (float)(img.Width / 2), (float)(img.Height - 16), stringFormat);
					graphics.Save();
				}
			}
			catch (Exception ex)
			{
				throw new Exception("ELABEL_GENERIC-1: " + ex.Message);
			}
		}
		internal static bool CheckNumericOnly(string Data)
		{
			int num = 18;
			string text = Data;
			string[] array = new string[Data.Length / num + ((Data.Length % num == 0) ? 0 : 1)];
			int i = 0;
			while (i < array.Length)
			{
				if (text.Length >= num)
				{
					array[i++] = text.Substring(0, num);
					text = text.Substring(num);
				}
				else
				{
					array[i++] = text.Substring(0);
				}
			}
			string[] array2 = array;
			bool result;
			for (int j = 0; j < array2.Length; j++)
			{
				string s = array2[j];
				long num2 = 0L;
				if (!long.TryParse(s, out num2))
				{
					result = false;
					return result;
				}
			}
			result = true;
			return result;
		}
		private string GetXML()
		{
			if (this.EncodedValue == "")
			{
				throw new Exception("EGETXML-1: Could not retrieve XML due to the barcode not being encoded first.  Please call Encode first.");
			}
			string result;
			try
			{
				using (BarcodeXML barcodeXML = new BarcodeXML())
				{
					BarcodeXML.BarcodeRow barcodeRow = barcodeXML.Barcode.NewBarcodeRow();
					barcodeRow.Type = this.Styles.ToString();
					barcodeRow.Text = this.Text;
					barcodeRow.EncodedValue = this.EncodedValue;
					barcodeRow.EncodingTime = this.EncodingTime;
					barcodeRow.ShowText = this.ShowText;
					barcodeRow.Forecolor = ColorTranslator.ToHtml(this.ForeColor);
					barcodeRow.Backcolor = ColorTranslator.ToHtml(this.BackColor);
					barcodeRow.CountryAssigningManufacturingCode = this.CAM_Code;
					barcodeRow.ImageWidth = this.Width;
					barcodeRow.ImageHeight = this.Height;
					using (MemoryStream memoryStream = new MemoryStream())
					{
						this.EncodedImage.Save(memoryStream, this.ImageFormat);
						barcodeRow.Image = Convert.ToBase64String(memoryStream.ToArray(), Base64FormattingOptions.None);
					}
					barcodeXML.Barcode.AddBarcodeRow(barcodeRow);
					StringWriter stringWriter = new StringWriter();
					barcodeXML.WriteXml(stringWriter, XmlWriteMode.WriteSchema);
					result = stringWriter.ToString();
				}
			}
			catch (Exception ex)
			{
				throw new Exception("EGETXML-2: " + ex.Message);
			}
			return result;
		}
		public static Image GetImageFromXML(BarcodeXML internalXML)
		{
			Image result;
			try
			{
				byte[] array = new byte[internalXML.Barcode[0].Image.Length];
				using (MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(internalXML.Barcode[0].Image)))
				{
					result = Image.FromStream(memoryStream);
				}
			}
			catch (Exception ex)
			{
				throw new Exception("EGETIMAGEFROMXML-1: " + ex.Message);
			}
			return result;
		}
		public static Image DoEncode(BarCodeStyles iBarCodeStyles, string Data)
		{
			Image result;
			using (BarcodeEdit barcodeEdit = new BarcodeEdit())
			{
				result = barcodeEdit.Encode(iBarCodeStyles, Data);
			}
			return result;
		}
		public static Image DoEncode(BarCodeStyles iBarCodeStyles, string Data, ref string XML)
		{
			Image result;
			using (BarcodeEdit barcodeEdit = new BarcodeEdit())
			{
				Image image = barcodeEdit.Encode(iBarCodeStyles, Data);
				XML = barcodeEdit.XML;
				result = image;
			}
			return result;
		}
		public static Image DoEncode(BarCodeStyles iBarCodeStyles, string Data, bool IncludeLabel)
		{
			Image result;
			using (BarcodeEdit barcodeEdit = new BarcodeEdit())
			{
				barcodeEdit.ShowText = IncludeLabel;
				result = barcodeEdit.Encode(iBarCodeStyles, Data);
			}
			return result;
		}
		public static Image DoEncode(BarCodeStyles iBarCodeStyles, string Data, bool IncludeLabel, int Width, int Height)
		{
			Image result;
			using (BarcodeEdit barcodeEdit = new BarcodeEdit())
			{
				barcodeEdit.ShowText = IncludeLabel;
				result = barcodeEdit.Encode(iBarCodeStyles, Data, Width, Height);
			}
			return result;
		}
		public static Image DoEncode(BarCodeStyles iBarCodeStyles, string Data, bool IncludeLabel, Color DrawColor, Color BackColor)
		{
			Image result;
			using (BarcodeEdit barcodeEdit = new BarcodeEdit())
			{
				barcodeEdit.ShowText = IncludeLabel;
				result = barcodeEdit.Encode(iBarCodeStyles, Data, DrawColor, BackColor);
			}
			return result;
		}
		public static Image DoEncode(BarCodeStyles iBarCodeStyles, string Data, bool IncludeLabel, Color DrawColor, Color BackColor, int Width, int Height)
		{
			Image result;
			using (BarcodeEdit barcodeEdit = new BarcodeEdit())
			{
				barcodeEdit.ShowText = IncludeLabel;
				result = barcodeEdit.Encode(iBarCodeStyles, Data, DrawColor, BackColor, Width, Height);
			}
			return result;
		}
		public static Image DoEncode(BarCodeStyles iBarCodeStyles, string data, bool IncludeLabel, Color DrawColor, Color BackColor, int Width, int Height, ref string XML)
		{
			Image result;
			using (BarcodeEdit barcodeEdit = new BarcodeEdit())
			{
				barcodeEdit.ShowText = IncludeLabel;
				Image image = barcodeEdit.Encode(iBarCodeStyles, data, DrawColor, BackColor, Width, Height);
				XML = barcodeEdit.XML;
				result = image;
			}
			return result;
		}
		public void Dispose()
		{
			try
			{
			}
			catch (Exception ex)
			{
				throw new Exception("EDISPOSE-1: " + ex.Message);
			}
		}
	}
}
